from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import filters
from rest_framework import permissions
# from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.serializers import AuthTokenSerializer
from . import restful
from .writerbook.writeai import writerAI
# Create your views here.
class WriteAI(APIView):
    """
    ?context=测试 传入内容<br>
    &times=1 生成数量<br>
    &num=20 每次生成字数<br>
    """
    permission_classes = [permissions.IsAuthenticated]

    def get(self, request, format=None):
        desc = self.request.query_params.get("context", '')
        times = self.request.query_params.get("times", '')
        num = self.request.query_params.get("num")
        # print(FN,LN,num,lenght)
        context = {}
        # context['names'] = self.getRandomName(request,FN,LN,num,lenght)
        context['data'] = self.getWrite(request,desc,times,num)
        return restful.success('获取数据成功！', context)
    
    #内置函数
    def getWrite(self, request,desc,times,num):
        data = {}
        if desc and times and num:
            times = int(times)
            num = int(num)
            data = writerAI(desc,times,num)
        elif desc and times:
            times = int(times)
            data = writerAI(desc,times,20)
        elif desc and num:
            num = int(num)
            data = writerAI(desc,1,num)
        elif desc:
            data = writerAI(desc)
        return data
    

